from selenium.webdriver.common.by import By

from pom.basePage import BasePage

class AdminCenter(BasePage):
    welcome_element = (By.XPATH,"/html/body/div/header/nav/div/ul/li/a/span")
    dashbord_link = (By.XPATH,'//*[@id="sidebar-menu"]/li[2]/a/span')
    article_link = (By.XPATH,'//*[@id="sidebar-menu"]/li[4]/a/span[1]')
    # page_link =(By.XPATH,'//*[@id="sidebar-menu"]/li[5]/a/span[1]')
    article_treeview = (By.XPATH, '//*[@id="sidebar-menu"]/li[4]/ul')
    article_manage = (By.XPATH,'//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a')
    write_article = (By.XPATH,'//*[@id="sidebar-menu"]/li[4]/ul/li[2]/a')
    category= (By.XPATH,'//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a')


    def __init__(self,driver,hostname):
        super().__init__(driver)
        self.url = f"http://{hostname}/jpress/admin/index".format(hostname=hostname)

    def goto_admincenter(self):
        self.goto_page(self.url)
    def navi_to_dashboard(self):
        self.click(self.dashbord_link)
    def article_treeview_is_folded(self):
        display = self.find_element(self.article_treeview).value_of_css_property('display')
        if display == 'block':
            return False
        else:
            return True
    def navi_to_article(self):
        self.click(self.article_link)

    def navi_to_article_treeview(self,locator):
        if self.article_treeview_is_folded():
            self.navi_to_article()
            self.wait(1)
        self.click(locator)
    def navi_to_article_manage(self):
        self.navi_to_article_treeview(self.article_manage)
    def navi_to_write_article(self):
        self.navi_to_article_treeview(self.write_article)
    def navi_to_category(self):
        self.navi_to_article_treeview(self.category)

    def get_welcome_text(self):
        return self.get_text(self.welcome_element)

    def wait_until_welcome_element_present(self):
        self.wait_until_element_present(self.welcome_element)
